﻿using DevExpress.XtraGrid.Demos.util;
using DiaoPaiDaYin;
using Luna小店.DTO;
using Luna小店.service;
using Luna小店.Utils;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.UC.面料库存
{
    public partial class Frm面料水洗标模板维护 : DevExpress.XtraEditors.XtraForm
    {
        private washingBarcodeIndexDTO washingBarcodeIndexDTO = new washingBarcodeIndexDTO();
        private String meterialDesc;
        private String composition;
        private washTemplateDTO washTemplateDTO = new washTemplateDTO();
        public Frm面料水洗标模板维护()
        {
            InitializeComponent();
        }
        public Frm面料水洗标模板维护(String materialNo,String meterialDesc,String composition)
        {
            InitializeComponent();
            washingBarcodeIndexDTO.materialCode = materialNo;
            this.meterialDesc = meterialDesc;
            this.composition = composition;
            this.LoadAll();
        }

        public void LoadAll()
        {
            this.label面料号.Text = "面料号：" + this.washingBarcodeIndexDTO.materialCode;
            this.label面料名称.Text = "面料名称：" + this.meterialDesc;
            this.label成分.Text = "成分：" + this.composition;
            this.comboBoxData();
        }
        /// <summary>
        /// 店铺们注入
        /// </summary>
        public void comboBoxData()
        {
            this.comboBox店铺.DataSource = FabricService.getShopfor水洗();
        }

        private void comboBox店铺_SelectedIndexChanged(object sender, EventArgs e)
        {
            washingBarcodeIndexDTO.shopId = this.comboBox店铺.SelectedValue.ToString();
            washingBarcodeIndexDTO.washingBarcodeTemplateId = RestCall.httpGetMethod(URL.接口url + "/material/getWashingBarcodeIndexByMaterialCodeAndShopId?materialCode=" + washingBarcodeIndexDTO.materialCode + "&shopId=" + washingBarcodeIndexDTO.shopId);
            if (!String.IsNullOrEmpty(washingBarcodeIndexDTO.washingBarcodeTemplateId))
            {
                this.Loud模板();
            }
            else
            {
                this.label水洗标模板.Text = "模板：";
            }
        }

        private void simpleButton下载_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.washingBarcodeIndexDTO.washingBarcodeTemplateId))
            {
                MessageBox.Show("暂无模板");
            }
            else
            {
                FolderBrowserDialog sFD = new FolderBrowserDialog();
                DialogResult rdr = sFD.ShowDialog();
                string fileName = sFD.SelectedPath;
                if (rdr == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(fileName))
                {
                    try
                    {
                        FileBinaryConvertHelper.StringToPath(this.washTemplateDTO.WASH_TEMPLATE, fileName + "\\" + this.washTemplateDTO.WASH_TEMPLATE_NAME);
                        MessageBox.Show("保存成功");
                    }
                    catch
                    {
                        MessageBox.Show("保存失败");
                    }
                }
            }

        }

        private void simpleButton上传_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String file = FileBinaryConvertHelper.PathToString(ofd.FileName);
                String filename = Path.GetFileName(ofd.FileName);
                String id = washTemplateDTO.ID;
                #region 首次上传
                if (String.IsNullOrEmpty(id))
                {
                    id = FileService.GetMD5HashFromFile(ofd.FileName);
                    #region 数据库中已存在该模板
                    if (FabricService.if水洗标模板已存在(id))
                    {
                        this.washingBarcodeIndexDTO.id = new FunctionHelper().Uuid;
                        this.washingBarcodeIndexDTO.washingBarcodeTemplateId = id;
                        try
                        {
                            RestCall.PostHttp(URL.接口url + "/material/saveWashingBarcodeIndex", FunctionHelper.JsonSerialization(this.washingBarcodeIndexDTO));
                            this.Loud模板();
                            MessageBox.Show("添加成功");
                        }
                        catch
                        {
                            MessageBox.Show("添加失败");
                        }
                    }

                    #endregion

                    #region 数据库中不存在该模板
                    else
                    {
                        //第一个模板
                        this.washTemplateDTO = new washTemplateDTO(id, file, filename, "", "");
                        this.washingBarcodeIndexDTO.id = new FunctionHelper().Uuid;
                        this.washingBarcodeIndexDTO.washingBarcodeTemplateId = id;
                        try
                        {
                            this.washTemplateDTO.上传模板();
                            RestCall.PostHttp(URL.接口url + "/material/saveWashingBarcodeIndex", FunctionHelper.JsonSerialization(this.washingBarcodeIndexDTO));
                            this.Loud模板();
                            MessageBox.Show("上传成功");
                        }
                        catch
                        {
                            MessageBox.Show("上传失败");
                        }
                    }

                    #endregion
                    
                }
                #endregion

                #region 非首次上传
                else
                {
                    id = FileService.GetMD5HashFromFile(ofd.FileName);
                    #region 数据库中已存在该模板
                    if (FabricService.if水洗标模板已存在(id))
                    {
                        this.washingBarcodeIndexDTO.id = new FunctionHelper().Uuid;
                        this.washingBarcodeIndexDTO.washingBarcodeTemplateId = id;
                        try
                        {
                            this.washTemplateDTO.更新模板();
                            RestCall.PostHttp(URL.接口url + "/material/saveWashingBarcodeIndex", FunctionHelper.JsonSerialization(this.washingBarcodeIndexDTO));
                            this.Loud模板();
                            MessageBox.Show("上传成功");
                        }
                        catch
                        {
                            MessageBox.Show("上传失败");
                        }
                    }

                    #endregion

                    #region 数据库中不存在该模板
                    else
                    {
                        //非首个模板
                        this.washTemplateDTO = new washTemplateDTO(id, file, filename, "", "");
                        this.washingBarcodeIndexDTO.id = new FunctionHelper().Uuid;
                        this.washingBarcodeIndexDTO.washingBarcodeTemplateId = id;
                        try
                        {
                            this.washTemplateDTO.更新模板();
                            RestCall.PostHttp(URL.接口url + "/material/saveWashingBarcodeIndex", FunctionHelper.JsonSerialization(this.washingBarcodeIndexDTO));
                            this.Loud模板();
                            MessageBox.Show("上传成功");
                        }
                        catch
                        {
                            MessageBox.Show("上传失败");
                        }
                    }

                    #endregion
                }

                #endregion
                
            }      
        }
        
        private void Loud模板()
        {
            this.washTemplateDTO = FileService.get水洗标模板(washingBarcodeIndexDTO.washingBarcodeTemplateId);
            this.pictureBox水洗标模板预览.Image = FileService.convertToImage(this.washTemplateDTO.WASH_TEMPLATE_IMAGE);
            this.label水洗标模板.Text = "模板：" + this.washTemplateDTO.WASH_TEMPLATE_NAME;
        }
    }
    /// <summary>
    /// 面料水洗标索引
    /// </summary>
    public class washingBarcodeIndexDTO
    {
        public String id { get; set; }
        public String materialCode { get; set; }
        public String shopId { get; set; }
        public String washingBarcodeTemplateId { get; set; }
        public washingBarcodeIndexDTO()
        {
            this.id = "";
            this.materialCode = "";
            this.shopId = "";
            this.washingBarcodeTemplateId = "";
        }
    }
    /// <summary>
    /// 水洗标模板数据
    /// </summary>
    public class washTemplateDTO
    {
        public String ID { get; set; }//模板id
        public String WASH_TEMPLATE { get; set; }//模板文件流
        public String WASH_TEMPLATE_NAME { get; set; }//模板文件名
        public String WASH_TEMPLATE_IMAGE { get; set; }//模板预览图片
        public String WASH_TEMPLATE_IMAGE_NAME { get; set; }//模板图片名

        public washTemplateDTO(DataRow dr)
        {
            this.ID = dr["ID"].ToString();
            this.WASH_TEMPLATE = dr["WASH_TEMPLATE"].ToString();
            this.WASH_TEMPLATE_NAME = dr["WASH_TEMPLATE_NAME"].ToString();
            this.WASH_TEMPLATE_IMAGE = dr["WASH_TEMPLATE_IMAGE"].ToString();
            this.WASH_TEMPLATE_IMAGE_NAME = dr["WASH_TEMPLATE_IMAGE_NAME"].ToString();
        }
        public washTemplateDTO()
        {
            this.ID = "";
            this.WASH_TEMPLATE = "";
            this.WASH_TEMPLATE_NAME = "";
            this.WASH_TEMPLATE_IMAGE = "";
            this.WASH_TEMPLATE_IMAGE_NAME = "";
        }

        public washTemplateDTO(string iD, string wASH_TEMPLATE, string wASH_TEMPLATE_NAME, string wASH_TEMPLATE_IMAGE, string wASH_TEMPLATE_IMAGE_NAME)
        {
            ID = iD;
            WASH_TEMPLATE = wASH_TEMPLATE;
            WASH_TEMPLATE_NAME = wASH_TEMPLATE_NAME;
            WASH_TEMPLATE_IMAGE = wASH_TEMPLATE_IMAGE;
            WASH_TEMPLATE_IMAGE_NAME = wASH_TEMPLATE_IMAGE_NAME;
        }
        /// <summary>
        /// 上传模板存入数据库
        /// </summary>
        /// <returns></returns>
        public washTemplateDTO 上传模板()
        {
            SQLerp.ExecuteSql("INSERT INTO `fragsmart-erp`.`t_wash_template_p` ( `ID`, `WASH_TEMPLATE`, `WASH_TEMPLATE_NAME`, `WASH_TEMPLATE_IMAGE`, `WASH_TEMPLATE_IMAGE_NAME` )\n" +
"VALUES\n" +
"	( '" + this.ID + "', '" + this.WASH_TEMPLATE + "', '" + this.WASH_TEMPLATE_NAME + "', '" + this.WASH_TEMPLATE_IMAGE + "', '" + this.WASH_TEMPLATE_IMAGE_NAME + "' );");
            return this;
        }

        public washTemplateDTO 更新模板()
        {
            SQLerp.ExecuteSql("UPDATE `fragsmart-erp`.`t_wash_template_p` \n" +
"SET `WASH_TEMPLATE` = '" + this.WASH_TEMPLATE + "',\n" +
"`WASH_TEMPLATE_NAME` = '" + this.WASH_TEMPLATE_NAME + "'\n" +
"WHERE\n" +
"	`ID` = '" + this.ID + "';");
            return this;
        }

        public washTemplateDTO 更新图片()
        {
            SQLerp.ExecuteSql("UPDATE `fragsmart-erp`.`t_wash_template_p` \n" +
"SET `WASH_TEMPLATE_IMAGE` = '" + this.WASH_TEMPLATE_IMAGE + "',\n" +
"`WASH_TEMPLATE_IMAGE_NAME` = '" + this.WASH_TEMPLATE_IMAGE_NAME + "' \n" +
"WHERE\n" +
"	`ID` = '" + this.ID + "';");
            return this;
        }
    }
}
